package cn.tedu.ivos2.geofence.service.impl;

import cn.tedu.ivos2.base.exception.ServiceException;
import cn.tedu.ivos2.base.response.StatusCode;
import cn.tedu.ivos2.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos2.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos2.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos2.geofence.pojo.entity.Geofence;
import cn.tedu.ivos2.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos2.geofence.service.GeofenceService;
import cn.tedu.ivos2.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos2.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos2.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏信息业务,参数:{}",geofenceQuery);
        List<GeofenceVO> list=geofenceMapper.selectGeofence(geofenceQuery);
        return list;
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("修改围栏状态业务,参数:{},{}",id,status);
        Geofence geofence = new Geofence();
        geofence.setId(id);
        geofence.setStatus(status);
        geofence.setUpdateTime(new Date());
        geofenceMapper.update(geofence);
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏业务,参数:{}",id);
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list=vehicleMapper.selectVehicle(vehicleQuery);
        if(list!=null&&list.size()>0){
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else{
            geofenceMapper.deleteById(id);
        }
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("新增围栏业务，参数：{}", geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        geofence.setStatus("1");//启用
        geofence.setCreateTime(new Date());
        geofenceMapper.insert(geofence);
    }
}
